﻿using System;
using System.Collections.Generic;
using System.Text;

using Brain.Interfaces;

namespace Brain
{
    public class Sensor : ISensor
    {
        private static int[] _connectionMatrix;//TODO: Make it independent singleton class??
        private static Random _rndGenerator = new Random();

        private bool _active;
        private List<Axon> _axonList;

        public Sensor(int maximunAxons, ref int[] connectionMatrix)
        {
            _axonList = new List<Axon>();
            _active = false;
            _connectionMatrix = connectionMatrix;

            for (int i = 0; i < maximunAxons; i++)
            {
                _axonList.Add(new Axon(_rndGenerator.Next(_connectionMatrix.Length)));
            }
        }

        public bool Active
        {
            get { return _active; }
            set 
            { 
                _active = value; 
                if (_active) OnActivate();
            }
        }
        
        public override string ToString()
        {
            string value = string.Empty;
            foreach (Axon ax in _axonList)
            {
                value += "|" + ax.DestinyId + "|";
            }
            return value;
        }

        private void OnActivate()
        {
            foreach (Axon ax in _axonList)
            {
                _connectionMatrix[ax.DestinyId] += 1;
            }
        }
    }
}
